#!/bin/sh

#####################################################################

if [ $1 ]
then
   echo
   echo "--------------------------------------------------------------"
   echo "--- Step 0. Figure out what we are doing"
   echo "--------------------------------------------------------------"
   echo
   SPUTNIK_DIR=$1      # "SPUTNIK_DIR" really is "the Kepler directory"
   echo "0.1 I will install Kepler and Sputnik into $SPUTNIK_DIR."
else
   echo "Build script for Sputnik (http://sputnik.freewisdom.org/)"
   echo ""
   echo "Usage:"
   echo ""
   echo "sh build_sputnik.sh SPUTNIK_DIR [BASE_URL]"
   echo ""
   echo "where: "
   echo "   SPUTNIK_DIR : the directory where Kepler and Sputnik will be installed"
   echo "                 (required)"
   echo "   BASE_URL    : the URL through which sputnik.cgi will be accessible"
   echo "                 (optional, defaults to /cgi-bin/sputnik.cgi)"
   echo ""
   exit
fi

TEMP_DIR=`pwd`"/tmp/"    # you can change this to /tmp/
echo "0.2 I will use $TEMP_DIR "
echo "    as the temporary directory."


WIKI_DATA_DIR="$SPUTNIK_DIR/wiki-data/"
if [ $2 ]
then
   BASE_URL=$2
   echo "0.3 You told me that Sputnik's URL will be $BASE_URL"
else
   BASE_URL="/cgi-bin/sputnik.cgi"
   echo "0.3 Sputnik's URL will be $BASE_URL (the default)."
fi

LOG_FILE="/tmp/log.txt"
echo "0.4 All the details will go to $LOG_FILE."
echo "" > $LOG_FILE

echo "\n    Ok, let's move on to the actual installation.\n"

#####################################################################
echo
echo "--------------------------------------------------------------"
echo "--- Step 1. Download, build, and install Kepler"
echo "--------------------------------------------------------------"
echo
### cd to $TEMP_DIR directory, to avoid polluting the local directory
### with temporary files

OLD_DIR=$PWD
cd $TEMP_DIR

### download and untar Kepler

ARCHIVE_NAME="kepler-1.1beta2.tar.gz"
DOWNLOAD_PREFIX="http://luaforge.net/frs/download.php/2720/"
DIR_NAME="kepler-1.1"

if [ -f $ARCHIVE_NAME ]
then
  echo "1.1 Skipping download - kepler archive already in $TEMP_DIR"
else
  echo "1.1 Downloading Kepler..."
  wget $DOWNLOAD_PREFIX$ARCHIVE_NAME
fi
if [ -f $DIR_NAME ]
then
  echo "1.2 Skipping untarring - there is already a kepler-1.1 directory"
else
  echo "1.2 Unpacking Kepler..."
  tar xvzf $ARCHIVE_NAME >> $LOG_FILE
fi
cd kepler-1.1


### configure, build and install kepler into $SPUTNIK_DIR
echo "1.3 Configuring and building... (this might take around minute)"
./configure --prefix=$SPUTNIK_DIR --launcher=cgi --without-readline --enable-lua --lua-suffix=5.1>> $LOG_FILE
make >> $LOG_FILE
make install >> $LOG_FILE

### return to the sputnik-wiki directory
cd $OLD_DIR

#####################################################################
echo
echo "--------------------------------------------------------------"
echo "--- Step 2. Install Sputnik"
echo "--------------------------------------------------------------"
echo

$SPUTNIK_DIR/bin/lua5.1 install.lua $SPUTNIK_DIR/etc/kepler/1.1/kepler_init.lua \
    $SPUTNIK_DIR $WIKI_DATA_DIR $BASE_URL

echo

echo "You should now be almost done.  Copy sputnik.cgi to your cgi-bin"
echo "directory, e.g.:"
echo "    sudo cp sputnik.cgi /usr/lib/cgi-bin/"
echo
echo "After that, access Sputnik at http://localhost$BASE_URL/sputnik.cgi"
echo
echo
